package cn.kgc.controller;

import cn.kgc.common.CrowdFundingConstant;
import cn.kgc.common.ResultEntity;
import cn.kgc.pojo.Hmold;

import cn.kgc.pojo.User;
import cn.kgc.service.MoldService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class MoldController {

    @Resource
    MoldService moldService;

    @RequestMapping(value = "/houseMold/query/for/search"/*,method = RequestMethod.POST*/)
    public String queryForSearch(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
            @RequestParam(value = "keyword", defaultValue = "") String keyword,
            Model model) {
        PageInfo<Hmold> pageInfo = moldService.queryForKeywordSearch(pageNum, pageSize, keyword);
        model.addAttribute(CrowdFundingConstant.ATTR_NAME_PAGE_INFO, pageInfo);
        return "body/mold/member-del";
    }


    //详细信息
    @RequestMapping("/houseMold/By/id")
    public String selectid(@RequestParam("id") Integer id, Model model) {
        Hmold xiangqing =moldService.selectByIdUser(id);
        model.addAttribute("xiang", xiangqing);
        return "body/mold/member-xiangxi";
    }


    //删除回显 批量
    //根据id查询
    @ResponseBody
    @RequestMapping("/mold/get/list/by/id/list")
    public ResultEntity<List<Hmold>> getRoleListByIdList(@RequestBody List<Integer> userIdList) {
        List<Hmold> userListByIdList = moldService.getUserListByIdList(userIdList);
        return ResultEntity.successWithoutData(userListByIdList);
    }

    //根据id删除  批量
    @ResponseBody
    @RequestMapping("/mold/batch/remove")
    public ResultEntity<String> batchRemove(@RequestBody List<Integer> userIdList) {
        moldService.batchRemove(userIdList);
        return ResultEntity.successWithoutData();
    }

    //单条删除
    @RequestMapping("delete/mold")
    public String deleteblog(@RequestParam("id") Integer id, Integer pageNum) {
        moldService.remove(id);
        return "redirect:/houseMold/query/for/search";
    }

    //增加
    @RequestMapping("/hold/save")
    public String saveAdmin(Hmold user, Integer pageNum) {
        moldService.saveUser(user);
        //删除成功跳转到查询页面  分页查询
        return "redirect:/houseMold/query/for/search";
    }


    //详细信息
    @RequestMapping("/hold/id")
    public String selectids(@RequestParam("id") Integer id, Model model) {
        Hmold xiangqing = moldService.selectByIdUser(id);
        model.addAttribute("xiang", xiangqing);
        return "body/mold/mold-update";
    }



    //修改
    @RequestMapping("/hold/update")
    public String updateAdmin(Hmold user, Integer pageNum) {
        moldService.updateUser(user);
        //当前页 存储过去
        return "redirect:/houseMold/query/for/search";
    }
}
